package Test.code;

import java.io.File;
import java.io.FileWriter;

import Test.tools.Utils;

public class ControllerGener {


	public static void controllerTest(String tableName,String tableBean,String currentBao) throws Exception{
		 String rn = "\r\n";
		 String []array = Utils.stringToArray(tableBean);
		 String src = 
			 
			 "package " + currentBao + ".controller;"+ rn+
			 rn +
		     "import java.util.List;"+ rn+
		     "import javax.servlet.http.HttpSession;"+ rn+
		     "import org.springframework.beans.factory.annotation.Autowired;"+ rn+
		     "import org.springframework.stereotype.Controller;"+ rn+
		     "import org.springframework.ui.ModelMap;"+ rn+
		     "import Test.tools.Page;"+ rn+
		     "import java.util.Map;"+ rn+
		     "import java.util.HashMap;"+ rn+
		     "import org.springframework.web.bind.annotation.PathVariable;"+ rn+
		     "import org.springframework.web.bind.annotation.RequestMapping;"+ rn+
		     "import org.springframework.web.bind.annotation.RequestMethod;"+ rn+
		     rn +
		     "import " + currentBao + ".bean."+Utils.upperFirstChar(tableName)+ ";"+ rn+
		     "import " + currentBao + ".service."+Utils.upperFirstChar(tableName)+ "Service;"+ rn+
		     rn +
		     "@Controller"+ rn+
		     "public class "+Utils.upperFirstChar(tableName)+ "Controller {"+ rn+
		          rn +
		 	 "      @Autowired"+ rn+
		 	 "      "+Utils.upperFirstChar(tableName)+ "Service service;"+ rn+
		 	      rn +
		 	 "      @RequestMapping(value = \"/"+tableName+"View.do\")"+ rn+
		 	 "      public String "+tableName+"View(ModelMap modelMap,String pageNo, String choice, HttpSession session){"+ rn+
		 	 
		 	 "			int currentPage = 1;"+ rn+
		 	 "			if (pageNo != null) {"+ rn+
		 	 "				currentPage = Integer.parseInt(pageNo);"+ rn+
		 	 "			}"+ rn+
		 	 "			int countNum = service.count"+Utils.upperFirstChar(tableName)+"();"+ rn+
			 "			Page _page = new Page(5, countNum, currentPage);"+ rn+
		 	 "			_page.setTotalRecord(countNum);"+ rn+
			 "			_page.setPageNumStart(_page.getPageNumStart());"+ rn+
			 "			_page.setPageNumEnd(_page.getPageNumEnd());"+ rn+
			 "			_page.setCurrentPage(currentPage);"+ rn+
			 "			Map<Object,Object> map = new HashMap<Object,Object>();"+ rn+
			 "			map.put(\"nowpage\", _page.getStart());"+ rn+
			 "			map.put(\"pagesize\", _page.getPageRecord());"+ rn+
			 "			List<"+Utils.upperFirstChar(tableName)+"> "+tableName+"List = null;"+ rn+
			 "			try {"+ rn+
			 "				"+tableName+"List = service.all"+Utils.upperFirstChar(tableName)+"(map);"+ rn+
			 "			} catch (Exception e) {"+ rn+
			 "				e.printStackTrace();"+ rn+
			 "			}"+ rn+
			 "			modelMap.put(\""+tableName+"List\", "+tableName+"List);"+ rn+
			 "			modelMap.put(\"showPage\", _page);"+ rn+
		 	 "          return \"back/jsp/"+tableName+"/"+tableName+"View\";"+ rn+
		 	 "     }"+ rn+
		 	      rn +
		 	 "      @RequestMapping(value = \"/"+tableName+"Del{"+array[0]+"}.do\")"+ rn+
		 	 "      public String "+tableName+"Del(@PathVariable(\""+array[0]+"\")int "+array[0]+" ,ModelMap modelMap,String pageNo, String choice, HttpSession session){"+ rn+
		 	 "             service.del"+Utils.upperFirstChar(tableName)+"("+array[0]+");"+ rn+
		 	 "             String message1=\"删除成功\";"+ rn+
		 	 "             String message2=\"请返回……\";"+ rn+
		 	 "             modelMap.put(\"message1\", message1);"+ rn+
		 	 "             modelMap.put(\"message2\", message2);"+ rn+
		 	 "            return \"redirect:/"+tableName+"View.do\";"+ rn+
		 	 "      }"+ rn+
		 	      rn +
		 	 "      @RequestMapping(value =\"/"+tableName+"GoAdd.do\")"+ rn+
		 	 "             public String "+tableName+"GoAdd(ModelMap modelMap,String pageNo, String choice, HttpSession session){"+ rn+
		 	 "             return \"back/jsp/"+tableName+"/"+tableName+"Add\";"+ rn+
		 	 "      }"+ rn+
		 	      rn +
		     "      @RequestMapping(value = \"/"+tableName+"Add.do\",method=RequestMethod.POST)"+ rn+
		     "      public String "+tableName+"Add("+Utils.upperFirstChar(tableName)+" form,ModelMap modelMap,String pageNo, String choice, HttpSession session){"+ rn+
		     "             service.add"+Utils.upperFirstChar(tableName)+"(form);"+ rn+
		     "             String message1=\"添加成功\";"+ rn+
		     "             String message2=\"请返回……\";"+ rn+
		     "             String  url=\""+tableName+"View.do\";"+ rn+
		     "             modelMap.put(\"message1\", message1);"+ rn+
		     "             modelMap.put(\"message2\", message2);"+ rn+
		     "             modelMap.put(\"url\", url);"+ rn+
		     "             return \"redirect:/"+tableName+"View.do\";"+ rn+
		     
		     "      }"+ rn+
		 	      rn +
		 	 "      @RequestMapping(value = \"/"+tableName+"GoUpdate{"+array[0]+"}.do\")"+ rn+
		 	 "      public String "+tableName+"GoUpdate(@PathVariable(\""+array[0]+"\")int "+array[0]+" ,ModelMap modelMap,String pageNo, String choice, HttpSession session){"+ rn+
		 	 "             "+Utils.upperFirstChar(tableName)+" "+tableName+" = service."+tableName+"("+array[0]+");"+ rn+
		 	 "             modelMap.put(\""+tableName+"\", "+tableName+");"+ rn+
		 	 "             return \"back/jsp/"+tableName+"/"+tableName+"Update\";"+ rn+
		 	 "      }"+ rn+
		 	      rn+
		 	 "      @RequestMapping(value = \"/"+tableName+"Update.do\",method=RequestMethod.POST)"+ rn+
		 	 "      public String "+tableName+"Update("+Utils.upperFirstChar(tableName)+" form,ModelMap modelMap,String pageNo, String choice, HttpSession session){"+ rn+
		 	 "             service.update"+Utils.upperFirstChar(tableName)+"(form);"+ rn+
		 	 "             String message1=\"修改成功\";"+ rn+
		 	 "             String message2=\"请返回……\";"+ rn+
		 	 "             String  url=\""+tableName+"View.do\";"+ rn+
		 	 "             modelMap.put(\"message1\", message1);"+ rn+
		 	 "             modelMap.put(\"message2\", message2);"+ rn+
		 	 "             modelMap.put(\"url\", url);"+ rn+
		 	 "             return \"redirect:/"+tableName+"View.do\";"+ rn+
		 	 "      }"+ rn+
		 	      rn +
			 " }"+ rn ;
			
			
		    String path  = System.getProperty("user.dir")+"/src/"+Utils.docToBackslash(currentBao)+"/controller/";
			File fpath = new File(path);
			if (!fpath.exists()) {
			   fpath.mkdirs();
		    }
			String fileName = System.getProperty("user.dir")+"/src/"+Utils.docToBackslash(currentBao)+"/controller/"+Utils.upperFirstChar(tableName)+"Controller.java";
			File f = new File(fileName);
			FileWriter fw = new FileWriter(f);
			fw.write(src);
			fw.flush();
			fw.close();
	 }

	public static void controllerTest_(String tableName,String firstTable,String tableBean,String currentBao) throws Exception{
		 String rn = "\r\n";
		 String []array = Utils.stringToArray(tableBean);
		 String src = 
			 
			 "package " + currentBao + ".controller;"+ rn+
			 rn +
		     "import java.util.List;"+ rn+
		     "import javax.servlet.http.HttpSession;"+ rn+
		     "import org.springframework.beans.factory.annotation.Autowired;"+ rn+
		     "import org.springframework.stereotype.Controller;"+ rn+
		     "import org.springframework.ui.ModelMap;"+ rn+
		     "import org.springframework.web.bind.annotation.PathVariable;"+ rn+
		     "import org.springframework.web.bind.annotation.RequestMapping;"+ rn+
		     "import org.springframework.web.bind.annotation.RequestMethod;"+ rn+
		     rn +
		     "import " + currentBao + ".bean."+Utils.upperFirstChar(tableName)+ ";"+ rn+
		     "import " + currentBao + ".service."+Utils.upperFirstChar(tableName)+ "Service;"+ rn+
		     rn +
		     "@Controller"+ rn+
		     "public class "+Utils.upperFirstChar(tableName)+ "Controller {"+ rn+
		          rn +
		 	 "      @Autowired"+ rn+
		 	 "      "+Utils.upperFirstChar(tableName)+ "Service service;"+ rn+
		 	      rn +
		 	 "      @RequestMapping(value = \"/"+tableName+"View{"+firstTable+"Id}.do\")"+ rn+
		 	 "      public String "+tableName+"View(@PathVariable(\""+firstTable+"Id\")String "+firstTable+"Id ,ModelMap modelMap,String pageNo, String choice, HttpSession session){"+ rn+
		 	 "            List<"+Utils.upperFirstChar(tableName)+"> "+tableName+"List = service.all"+Utils.upperFirstChar(tableName)+"("+firstTable+"Id);"+ rn+
		 	 "             "+Utils.upperFirstChar(tableName)+"  "+tableName+" = new  "+Utils.upperFirstChar(tableName)+"(); "+ rn+
		 	 "             "+tableName+".set"+Utils.upperFirstChar(firstTable)+"Id("+firstTable+"Id);"+ rn+
		 	 "             modelMap.put(\""+tableName+"\",  "+tableName+");"+ rn+
		 	 "             modelMap.put(\""+tableName+"List\", "+tableName+"List);"+ rn+
		 	 "           return \"back/jsp/"+tableName+"/"+tableName+"View\";"+ rn+
		 	 "     }"+ rn+
		 	      rn +
		 	 "      @RequestMapping(value = \"/"+tableName+"Del{"+array[0]+"}.do\")"+ rn+
		 	 "      public String "+tableName+"Del(@PathVariable(\""+array[0]+"\")int "+array[0]+" ,ModelMap modelMap,String pageNo, String choice, HttpSession session){"+ rn+
		 	 "            "+Utils.upperFirstChar(tableName)+" "+tableName+" = service."+tableName+"("+array[0]+");"+ rn+
		 	 "             service.del"+Utils.upperFirstChar(tableName)+"("+array[0]+");"+ rn+
		 	 "             String message1=\"删除成功\";"+ rn+
		 	 "             String message2=\"请返回……\";"+ rn+
		 	 "             String  url=\""+tableName+"View\"+"+tableName+".get"+Utils.upperFirstChar(firstTable)+"Id()+\".do\";"+ rn+
		 	 "             modelMap.put(\"message1\", message1);"+ rn+
		 	 "             modelMap.put(\"message2\", message2);"+ rn+
		 	 "             modelMap.put(\"url\", url);"+ rn+
		 	 "            return \"infomationShow\";"+ rn+
		 	 "      }"+ rn+
		 	      rn +
		 	 "      @RequestMapping(value =\"/"+tableName+"GoAdd{"+firstTable+"Id}.do\")"+ rn+
		 	 "             public String "+tableName+"GoAdd(@PathVariable(\""+firstTable+"Id\")String "+firstTable+"Id ,ModelMap modelMap,String pageNo, String choice, HttpSession session){"+ rn+
		 	 "             "+Utils.upperFirstChar(tableName)+"  "+tableName+" = new  "+Utils.upperFirstChar(tableName)+"(); "+ rn+
		 	 "             "+tableName+".set"+Utils.upperFirstChar(firstTable)+"Id("+firstTable+"Id);"+ rn+
		 	 "             modelMap.put(\""+tableName+"\",  "+tableName+");"+ rn+
		 	 "             return \"back/jsp/"+tableName+"/"+tableName+"Add\";"+ rn+
		 	 "      }"+ rn+
		 	      rn +
		     "      @RequestMapping(value = \"/"+tableName+"Add.do\",method=RequestMethod.POST)"+ rn+
		     "      public String "+tableName+"Add("+Utils.upperFirstChar(tableName)+" form,ModelMap modelMap,String pageNo, String choice, HttpSession session){"+ rn+
		     "             service.add"+Utils.upperFirstChar(tableName)+"(form);"+ rn+
		     "             String message1=\"添加成功\";"+ rn+
		     "             String message2=\"请返回……\";"+ rn+
		     "             String  url=\""+tableName+"View\"+form.get"+Utils.upperFirstChar(firstTable)+"Id()+\".do\";"+ rn+
		     "             modelMap.put(\"message1\", message1);"+ rn+
		     "             modelMap.put(\"message2\", message2);"+ rn+
		     "             modelMap.put(\"url\", url);"+ rn+
		     "             return \"infomationShow\";"+ rn+
		     "      }"+ rn+
		 	      rn +
		 	 "      @RequestMapping(value = \"/"+tableName+"GoUpdate{"+array[0]+"}.do\")"+ rn+
		 	 "      public String "+tableName+"GoUpdate(@PathVariable(\""+array[0]+"\")int "+array[0]+" ,ModelMap modelMap,String pageNo, String choice, HttpSession session){"+ rn+
		 	 "            "+Utils.upperFirstChar(tableName)+" "+tableName+" = service."+tableName+"("+array[0]+");"+ rn+
		 	 "           modelMap.put(\""+tableName+"\", "+tableName+");"+ rn+
		 	 "           return \"back/jsp/"+tableName+"/"+tableName+"Update\";"+ rn+
		 	 "      }"+ rn+
		 	      rn+
		 	 "      @RequestMapping(value = \"/"+tableName+"Update.do\",method=RequestMethod.POST)"+ rn+
		 	 "      public String "+tableName+"Update("+Utils.upperFirstChar(tableName)+" form,ModelMap modelMap,String pageNo, String choice, HttpSession session){"+ rn+
		 	 "             service.update"+Utils.upperFirstChar(tableName)+"(form);"+ rn+
		 	 "             String message1=\"修改成功\";"+ rn+
		 	 "             String message2=\"请返回……\";"+ rn+
		 	 "             String  url=\""+tableName+"View\"+form.get"+Utils.upperFirstChar(firstTable)+"Id()+\".do\";"+ rn+
		 	 "             modelMap.put(\"message1\", message1);"+ rn+
		 	 "             modelMap.put(\"message2\", message2);"+ rn+
		 	 "             modelMap.put(\"url\", url);"+ rn+
		 	 "             return \"infomationShow\";"+ rn+
		 	 "      }"+ rn+
		 	      rn +
			 "}"+ rn ;
			
			
		    String path  = System.getProperty("user.dir")+"/src/"+Utils.docToBackslash(currentBao)+"/controller/";
			File fpath = new File(path);
			if (!fpath.exists()) {
			   fpath.mkdirs();
		    }
			String fileName = System.getProperty("user.dir")+"/src/"+Utils.docToBackslash(currentBao)+"/controller/"+Utils.upperFirstChar(tableName)+"Controller.java";
			File f = new File(fileName);
			FileWriter fw = new FileWriter(f);
			fw.write(src);
			fw.flush();
			fw.close();
	 }

	
	
}
